<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminlogs.php
#	FUNCTION:		Custom handler for the Admin Logs page
#	AUTHOR:			Cameron Morrow
#	CREATED:		12/09/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# D A T A   F O R   L O G S
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Field to group browse on
$browse_group_field = "";

# Is index numeric
$index_numeric = true;

# Index field
$index_field = "l_id";

# Type name
$type_name = "Log Item";

# Plural
$type_name_plural = "Log Items";

# Class name
$name_class = "logitem";

# Custom Columns
$custom_columns = array();

# Browse SQL
$browse_sql_additional_where_clauses = array();

# Edit SQL
$edit_sql_additional_selects = array();
$edit_sql_additional_tables = array();
$edit_sql_additional_where_clauses = array();

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# F O R M A T T I N G
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Get table property information for this data
$data_formatting = getRecord("SELECT * FROM " . $PROJECT_DB_TABLES["table_properties"] . " WHERE tp_table = '" . $PROJECT_DB_TABLES["logs"] . "' ORDER BY tp_browsesortorder ASC");

# Create TableProperties
$data_properties = new TableProperties();

# Add properties
foreach ($data_formatting as $row_format) {
	$data_properties -> addProperty(new TableProperty($row_format["tp_field"], $row_format));
}

# Add custom columns
foreach ($custom_columns as $custom_column) {
	$data_properties -> addCustomColumn($custom_column[0], $custom_column[1]);
}


# C H E C K   F O R   P U R G E
if (@$_POST["purge-y"]) {

	# Get purge date
	$purge_date = $_POST["purge-y"] . $_POST["purge-m"] . $_POST["purge-d"] . $_POST["purge-h"] . "0000";

	# SQL
	$sql = "DELETE FROM " . $PROJECT_DB_TABLES["logs"] . " WHERE l_date < '" . $purge_date . "'";

	# Execute
	if (executeSQLQuery($sql)) {
		addMessage("<p>Logs dated before " . formatDate($purge_date) . " have been deleted.</p>");
	} else {
		addMessage("<p>An error occured and the logs could not be purged.</p>", 2);
	}
}


# C R E A T E   B R O W S E   S Q L
$browse_sql = "SELECT " . $PROJECT_DB_TABLES["logs"] . ".* FROM " . $PROJECT_DB_TABLES["logs"];

# C H E C K   L I M I T S
if (@$_POST["limit"]) {

	# For each limit type
	$limit_types = array();

	foreach ($LOG_TYPE_GROUPS as $log_type_group_id => $log_type_group) {

		# If found
		if (@$_POST["loglimit" . $log_type_group_id]) {

			# Add types for this group
			foreach ($log_type_group["groups"] as $type) {
				$limit_types[] = $type;
			}
		}
	}

	# If any found
	if (count($limit_types) > 0) {

		$browse_sql .= " WHERE ";

		foreach ($limit_types as $counter => $type) {
			$browse_sql .= " l_type = " . $type;
			$browse_sql .= ($counter < count($limit_types) - 1) ? " OR " : "";
		}
	}
}

# End SQL
$browse_sql .= " ORDER BY l_date DESC";

# Get current data
$current_data = getRecord($browse_sql, $index_field);

# Search form
addContent("<h2 class=\"search\">Limits:</h2><form name=\"searchitemform\" id=\"searchitemform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
	"<input type=\"hidden\" name=\"result\" id=\"result\" value=\"1\" />" .
	"<input type=\"hidden\" id=\"limit\" name=\"limit\" value=\"1\" />" .
	"<p>Limit to: ");

# Add limits
foreach ($LOG_TYPE_GROUPS as $log_type_group_id => $log_type_group) {
	addContent("<input type=\"checkbox\" name=\"loglimit" . $log_type_group_id . "\" id=\"loglimit" . $log_type_group_id . "\" " . (@$_POST["loglimit" . $log_type_group_id] || @$_POST["limit"] == false ? "checked=\"checked\" " : "") . "/> " . "<label for=\"loglimit" . $log_type_group_id . "\">" . $log_type_group["name"] . "</label> ");
}

# Close form
addContent("</p><p><input type=\"submit\" value=\"Limit to just these types\" /></p>" .
	"</form>");

# Get current time
$now_date = getFullDate();
$year = substr($now_date, 0, 4);
$month = substr($now_date, 4, 2);
$day = substr($now_date, 6, 2);
$hour = substr($now_date, 8, 2);

# Purge form
addContent("<h2 class=\"purge\">Purge old logs</h2><form name=\"purgeform\" id=\"purgeform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
	"<p>Purge log items from before: " .
	createSelector("purge-d", $DAY_ARRAY, $DAY_ARRAY, findInArray($DAY_ARRAY, $day)) .
	" " .
	createSelector("purge-m", $MONTH_ARRAY, $MONTH_NUM_ARRAY, findInArray($MONTH_NUM_ARRAY, $month)) .
	"" .
	createSelector("purge-y", $YEAR_ARRAY, $YEAR_ARRAY, findInArray($YEAR_ARRAY, $year)) .
	", " .
	createSelector("purge-h", $HOUR_ARRAY, $HOUR_ARRAY, findInArray($HOUR_ARRAY, $hour)) .
	":00" .
	" <input type=\"submit\" value=\"Purge\" /></p>" .
	"</form>");

# Draw the table
addContent($data_properties -> drawBrowseTable($current_data, (($SEARCH_TERM == "") ? "Browsing " : "Search results in ") . $type_name_plural, $browse_group_field, $name_class));

# Draw the action form
addContent(generateActionForm());


?>